<?php

require_once(dirname(__FILE__).'/../config/ProjectConfiguration.class.php');
$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'dev', true);
sfContext::createInstance($configuration);

$databaseManager = new sfDatabaseManager($configuration);
$databaseManager->loadConfiguration();

if (empty($argc))
{
  die("Please specify a url.");
}
$site_url = $argv[1];

// remove trailing slash
if (substr($site_url, -1) == "/")
{
	$site_url = substr($site_url, 0, -1);
}

$site_table = Doctrine_Core::getTable("Site");

// if we cant find it in the database already..
if (!($site = $site_table->findOneByUrl($site_url)))
{
	// create a new one
	$site = new Site();
	$site->setUrl($site_url);
}

if ($site)
{
	// set the status to indexing and delete any current pages
	$site->setStatus(Site::INDEXING);
	$site->save();
	$site->deletePages();

	$parser = new UrlParser($site->getUrl());
	foreach($parser->parse() as $url => $risk)
	{
		$page = new Page();
		$page->setSite($site);
		$page->setUrl($url);
		$page->setRisk($risk);
		$page->save();

	}
	$site->setLastIndexed(date("Y-m-d H:i:s"));
	$site->setStatus(Site::WAITING);
	$site->save();
}else {
	die("Unknown problem with site");
}
